<%-- 
    Document   : ProjectInfo
    Created on : Jul 21, 2011, 10:02:50 AM
    Author     : sugia
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="ProjectMgtSystem.Entities.*" %>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="Content-Style-Type" content="text/css" />
        <link href="css/style1.css" rel="stylesheet" type="text/css" />        
        <script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
        <script type="text/javascript" src="js/ui.core.js"></script>
        <script type="text/javascript" src="js/ui.datepicker.js"></script>
        <script>
            $(document).ready(function(){
                show_error("<%= request.getAttribute("errorMessage").toString()%>");                 
            });
        </script>
    </head>

    <!------------------------------------------------------------------------------------------------------->                            
    <!----------------------------- javascript begins here -------------------------------------------->
    <!------------------------------------------------------------------------------------------------------->
    <script type="text/javascript">
        $(document).ready(function(){
            $(".insBtt").click(function(){                                        
                $(".insert").toggle();
            });                                    
        });
    
        //define function to be executed on document ready
        $(function(){

            //create the date picker
            $("#pSD").datepicker();
            $("#pED").datepicker();
        });
    
    </script>

    <script type="text/javascript">
        function show_confirm()
        {
            if(confirm("Are your sure?")){
                window.location("ProjectAction.do?method=Delete");
            }
            else
                return false;
        }
    </script>

    <script type="text/javascript">
        function show_error(val)
        {    
            if(val != "null"){
                var msgErr = val;            
                if(msgErr != "" && msgErr!="1"){
                    /*if(msgEr.toString().indexOf("Insert_", 1))
                {
                    $(".insert").toggle(true);                                                
                    msgErr = msgErr.toString().substringData(8, msgEr.toString().length - 7);
                }*/
                    if(msgErr != "-1") {
                        alert(msgErr);
                    }
                    else{
                        alert("Data error!!!")
                    }                
                }                                    
                else
                    return false;
            }
            else{
                return false;
            }
        }   
        
        function isValidDate(sText) {
            var reDate = /(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12][0-9]|3[01])\/(?:19|20\d{2})/;
            return reDate.test(sText);
        }
        function validateForm()
        {
            var x= $("#ID").val();              
            var y= $("#Name").val();           
            var z= $("#TLID").val();           
            var pSD= $("#pSD").val();           
            var pED= $("#pED").val();           
            var msgErr ="";
            if (x==null || x=="")
            {
                msgErr = "Project ID must be filled out.";                
            }
            if (y==null || y=="")
            {
                msgErr = msgErr + "\nProject Name must be filled out.";                
            }   
            if (z=="-1")
            {
                msgErr = msgErr + "\nTeam Leader must be selected.";                
            }
            if (pSD=="" || !isValidDate(pSD) || pSD.toString().length != 10)
            {
                msgErr = msgErr + "\nPlan Start Date is invalid.";                
            }
            if (pED=="" || !isValidDate(pED) || pED.toString().length != 10)
            {                
                msgErr = msgErr + "\nPlan End Date is invalid.";                
            }
            var strSDate = pSD.split("/"); 
            var strEDate = pED.split("/"); 
            var pSDate = parseInt(strSDate[2]+strSDate[0]+strSDate[1], 10);                
            var pEDate = parseInt(strEDate[2]+strEDate[0]+strEDate[1], 10);
            if(pSDate > pEDate){
                 msgErr = msgErr + "\nPlan Start Date is greater than Plan End Date.";                
            }   
            if(msgErr != ""){
                alert(msgErr);
                return false;
            }                
        }
    </script>

    <!----------------------------- javascript end here -------------------------------------------->
    <body id="page1">
        <!-- header -->
        <div class="inner_copy"></div> 
        <div id="header">
            <div class="container">
                <!-- .logo -->
                <div class="logo">
                    <a href="index.html"><img src="images/logo1.jpg" alt="" /></a>
                </div>
                <!-- /.logo -->
                <!-- .nav -->
                <ul class="nav">
                    <li><a href="AdminHomepage.jsp">HOME</a></li>
                    <li><html:link href="engineer.do?method=View">ENGINEER</html:link></li>
                        <li><a href="#" class="current">PROJECT</a></li>
                        <li><html:link href="TaskAction.do?method=View">TASK</html:link></li>
                    </ul>
                    <!-- /.nav -->    
                    <form action="" id="search-form">
                        <fieldset>
                            <p>Back to <a href="login.jsp">log in</a> page.</p>
                        </fieldset>
                    </form>
                </div>
            </div>
            <!-- content -->
            <div id="content">
                <div class="container">
                    <div class="inside">                   
                        <div class="wrapper">                        
                            <div class="mainContent">
                                <!------------------------------------------------------------------------------------------------------->                            
                                <!----------------------------- Our main content begins here -------------------------------------------->
                                <!------------------------------------------------------------------------------------------------------->

                                <div class="add"><button class="insBtt">Add New Project</button></div>
                                <div class="insert">
                                    <table >
                                    <html:form action="/ProjectAction">
                                        <tr>
                                            <th scope="col">Project ID</th>
                                            <td><html:text property="projectID" styleId="ID"/></td>
                                        </tr> 
                                        <tr>
                                            <th scope="col">Project Name</th>
                                            <td><html:text property="projectName" styleId="Name"/></td>
                                        </tr>  
                                        <tr>
                                            <th scope="col">Team Leader</th>
                                            <td>

                                                <html:select property="teamLeader" styleId="TLID"> 
                                                    <html:option value="-1">Choose a value</html:option>
                                                    <%
                                                        java.util.ArrayList<Engineer> eList = (java.util.ArrayList<Engineer>) request.getAttribute("engineerList");
                                                        if (eList.size() > 0) {
                                                            for (Engineer en : eList) {
                                                    %>                                                     }
                                                    <html:option value="<%= en.getEngineerID()%>"><%= en.getEngineerID()%> _ <%= en.getFirstName()%> <%= en.getLastName()%></html:option>
                                                    <%
                                                            }
                                                        }
                                                    %>                                                                                                        
                                                </html:select> 
                                            </td>
                                        </tr> 
                                        <tr>
                                            <th scope="col">Description</th>
                                            <td><html:textarea property="description"/></td>
                                        </tr>  
                                        <tr>
                                            <th scope="col">Plan Start Date</th>
                                            <td><html:text property="planStartDate" styleId="pSD"/></td>
                                        </tr>         
                                        <tr>
                                            <th scope="col">Plan End Date</th>
                                            <td><html:text property="planEndDate" styleId="pED"/></td>
                                        </tr>         

                                        <tr>
                                            <td><html:submit property="method" onclick="return validateForm()"><bean:message key="ProjectAction.insert"/></html:submit></td>
                                            <td><html:messages id="msg" message="true">
                                                    <bean:write name="msg"/><br></html:messages></td>
                                            </tr>
                                    </html:form>
                                </table>
                            </div>

                            <table id="mytable" cellspacing="0">
                                <tr>
                                    <th scope="col">ID</th>
                                    <th scope="col">Project Name</th>
                                    <th scope="col">Team Leader</th>
                                    <th scope="col">Description</th> 
                                    <th scope="col">Plan Start Date</th>   
                                    <th scope="col">Plan End Date</th>   
                                    <%
                                        java.util.ArrayList<Project> pList = (java.util.ArrayList<Project>) request.getAttribute("projectList");
                                        for (Project pr : pList) {%>
                                    <html:form action="/ProjectAction">
                                    <tr>

                                        <%
                                            out.print("<td>" + pr.getProjectID() + "</td>");
                                            out.print("<td>" + pr.getProjectName() + "</td>");
                                            out.print("<td>" + pr.getTeamLeader().getEngineerID() + "_" + pr.getTeamLeader().getFirstName() + " " + pr.getTeamLeader().getLastName() + "</td>");
                                            out.print("<td>" + pr.getDescription() + "</td>");
                                            out.print("<td>" + pr.getPlanStartDate() + "</td>");
                                            out.print("<td>" + pr.getPlanEndDate() + "</td>");
                                        %>   
                                        <html:hidden property="projectID" value="<%=pr.getProjectID()%>"/>
                                        <td><html:submit property="method" onclick="return show_confirm()"><bean:message key="ProjectAction.delete"/></html:submit></td>
                                        <td><html:submit property="method"><bean:message key="ProjectAction.select"/></html:submit></td>

                                            <td><html:submit property="method"><bean:message key="ProjectAction.viewTasks"/></html:submit></td>  

                                        <%
                                            out.print("</tr>");
                                        %>
                                    </html:form>   
                                    <%
                                        }
                                    %>

                                </tr>
                            </table>

                            <!-----------------------------End main content-------------------------------------------------------------->

                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- footer -->
        <div id="footer">
            <div class="container">
                <div class="inside">
      	Copyright &copy; Team A POSCO-ICT
                </div>
            </div>
        </div>
    </body>
</html>
